package com.iotimc.attendancesystem.attendance.service;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.iotimc.attendancesystem.event.KeyEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * @author david
 * @date 2017/12/12
 */

public class WindowService extends AccessibilityService {
    private static final String TAG = "WindowService";
    String[] PACKAGES = {"com.android.settings"};

    @Override
    protected void onServiceConnected() {
        Log.i(TAG, "config success!");
        AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
        // accessibilityServiceInfo.packageNames = PACKAGES;
        accessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        accessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        accessibilityServiceInfo.notificationTimeout = 1000;
        setServiceInfo(accessibilityServiceInfo);
    }


    /**
     * 页面变化回调事件
     *
     * @param event event.getEventType() 当前事件的类型;
     *              event.getClassName() 当前类的名称;
     *              event.getSource() 当前页面中的节点信息；
     *              event.getPackageName() 事件源所在的包名
     */
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.i(TAG, "onAccessibilityEvent: " + event.getPackageName());
        int eventType = event.getEventType();
        Log.d(TAG, "the eventType is :" + eventType);

        //得到被点击的对象
        AccessibilityNodeInfo accessibilityNodeInfo = event.getSource();
        if (accessibilityNodeInfo!=null) {
            Log.d(TAG, "the accessibilityNodeInfo " + accessibilityNodeInfo.getText());
        }
        // getRootInActiveWindow();  获得整个窗口对象

        if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED ||
                eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { //当当前窗口发生变化时候，重新遍历，否则后面的控件找不到
            AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
            if (rootInActiveWindow == null) {
                Log.d(TAG, "the rootInActiveWindow is null");
            } else {
                recycle(rootInActiveWindow);
            }
        }
    }

    public void recycle(AccessibilityNodeInfo rootInActiveWindow) {
        //只有一个子元素
        if (rootInActiveWindow.getChildCount() == 0) {
            Log.d(TAG, "the rootInActiveWindow count is : 0");
        } else {
            if (rootInActiveWindow != null) {
                //匹配Text获取节点
                Log.i(TAG, "onKeyEvent: " + rootInActiveWindow.getChildCount());
            }
        }
    }

    @Override
    public void onInterrupt() {
        Log.i(TAG, "onInterrupt: ");
    }

    @Subscribe
    public void onKeyEvent(KeyEvent event) {


    }


    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
